package com.svo.md.service.impl;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.b510.bean.Person;
import com.b510.service.impl.PersonRowMapper;
import com.svo.md.bean.UserEntity;
import com.svo.md.service.UserService;

public class UserServiceBean implements UserService {
	/**
	 * 数据源
	 */
	private DataSource dataSource;
	/**
	 * spring提供的jdbc操作辅助类
	 */
	private JdbcTemplate jdbcTemplate;
	
	// 设置数据源
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	@Override
	public void save(UserEntity user) {
		jdbcTemplate.update("insert ignore into md_user(devId,startTime,endTime)values(?,?,?)",
                new Object[] { user.getDevId(), user.getStartTime(),
                		user.getEndTime()}, new int[] { java.sql.Types.VARCHAR,
                         java.sql.Types.TIMESTAMP , java.sql.Types.TIMESTAMP});

	}

	@Override
	public void update(UserEntity user) {
		// TODO Auto-generated method stub

	}

	@Override
	public UserEntity getPerson(String devId) {
		try {
			UserEntity person = (UserEntity) jdbcTemplate.queryForObject(
			        "select * from md_user where devId=?", new Object[] { devId },
			        new int[] { java.sql.Types.VARCHAR }, new UserRowMapper());
			return person;
		} catch (Exception e) {
			System.out.println("personServiceBean:"+e.getMessage());
			return null;
		}
	}

}
